// api
import { getList, removeIdsApi, methodPostApi, methodGetApi } from '@/api/publicApi'
// js
import { getDownLoadFile, downLoadFileOther } from '@/utils/commonUtils/zipdownload'// rem转换的js文件
// 混入
import { publicMixins } from '@/mixins/publicMixins/publicMixins'
// listMixins
export const listMixins = {
  mixins: [publicMixins],
  data() {
    return {
      // 遮罩层
      listLoading: false,
      // 表格数据
      list: [],
      // 总数
      listTotal: 0,
      // 有复选框的情况下 选中的数据
      selectTableList: []
    }
  },
  updated() {
    this.resizeViewConfig()
  },
  mounted() {
    window.addEventListener('resize', this.resizeViewConfig())
  },
  methods: {
    /**
     * @description: 列表统计
     * @Author: baoxuhui
     * @param {*} url//联调的后端接口路径
     * @param {*} data//接口所需的入参
     * @param {*} queryParams//渲染实体值
     * @return {*}
     */
    getStatisticsMixins(url, data, queryName) {
      methodGetApi(url, data).then(res => {
        this[`${queryName}`] = res.data
      })
    },
    /**
     * @description: 列表
     * @Author: baoxuhui
     * @param {*} url//联调的后端接口路径
     * @param {*} queryParams//接口所需的入参
     * @param {*} pageBool//是否分页(布尔值)   默认：true
     * @return {*}
     */
    getListMixins(url, queryParams, pageBool, handleTreeBool, hanleTreeIdName) {
      return new Promise((resolve) => {
        let newPageBool = true
        if (this.sentencedEmpty(pageBool)) {
          newPageBool = pageBool
        }
        this.listLoading = true
        getList(url, queryParams).then(response => {
          if (newPageBool) {
            this.list = response.rows
            this.listTotal = response.total
          } else {
            this.list = response.data
          }
          if (this.sentencedEmpty(handleTreeBool)) {
            this.list = this.handleTree(response.data, hanleTreeIdName)
          }
          this.listLoading = false
        })
        resolve(this.list)
      })
    },
    /**
     * @description: 树形列表
     * @Author: baoxuhui
     * @param {*} url//联调的后端接口路径
     * @param {*} queryParams//接口所需的入参
     * @param {*} treeId//树形结构的Id
     * @return {*}
     */
    getTreeListMixins(url, queryParams, treeId) {
      this.listLoading = true
      getList(url, queryParams).then(response => {
        this.list = this.handleTree(response.data, treeId)
        this.listLoading = false
      })
    },
    /**
     * @description: 删除事件
     * @Author: baoxuhui
     * @param {*} url//接口路径
     * @param {*} queryParams//入参
     * @return {*}
     */
    handleDeleteMixins(url, queryParams) {
      this.$confirm('确认删除吗?', '警告', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(function() {
        return removeIdsApi(url, queryParams)
      }).then(() => {
        if (this.$refs['advancedQueryShrinkComponent']) {
          this.$refs['advancedQueryShrinkComponent'].handleResetFun()
        } else {
          this.getList()
        }
        this.msgAlt('删除成功', 'success')
      })
    },
    /**
     * @description: switch状态变更
     * @Author: baoxuhui
     * @param {*} url//接口路径
     * @param {*} params//入参
     * @param {*} row//所需的obj数据
     * @return {*}
     */
    handleSwitchChangeMixins(url, params, text) {
      this.$confirm(
        '确认要' + text + '吗?',
        '提示',
        {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }
      )
        .then(function() {
          methodPostApi(url, params).then(res => {
            this.msgAlt(text + '成功', 'success')
            this.$refs['advancedQueryShrinkComponent'].handleResetFun()
          })
        }).catch(function() {
          this.$refs['advancedQueryShrinkComponent'].handleResetFun()
        })
    },
    /**
     * @description: confirm操作
     * @Author: baoxuhui
     * @param {*} url//接口路径
     * @param {*} params//入参
     * @param {*} text//事件的中文
     * @param {*} methodTypetext//接口请求类型默认:post
     * @return {*}
     */
    handleConfirmMixins(url, params, text, methodType) {
      this.$confirm(
        '确认要' + text + '吗?',
        '提示',
        {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }
      )
        .then(function() {
          if (methodType === 'get') {
            methodGetApi(url, params).then(res => {
              this.msgAlt(text + '成功', 'success')
              this.$refs['advancedQueryShrinkComponent'].handleResetFun()
            })
          } else {
            methodPostApi(url, params).then(res => {
              this.msgAlt(text + '成功', 'success')
              this.$refs['advancedQueryShrinkComponent'].handleResetFun()
            })
          }
        }).catch(() => {
          this.msgAlt(`已取消${text}`, 'info')
        })
    },
    /**
     * @description: 跳转页面事件
     * @Author: baoxuhui
     * @param {*} type // add:新增界面，eidt:修改界面，view:查看界面
     * @param {*} queryParams // 路径的跟参
     * @param {*} url // 自定义路由
     * @return {*}
     */
    handleJumpMixins(type, queryParams, url) {
      var path = this.$route.meta.parentPath
      if (url) {
        path = url
      } else{
        if (type !== 'view') {
          path = path + 'Form'
        } else {
          path = path + 'View'
        }
      }
      this.$router.push({ path: path, query: queryParams })
    },

    /**
     * @description: 导出文件流
     * @Author: baoxuhui
     * @param {*} path//跳转的路由
     * @param {*} param//路径的跟参
     * @param {*} fileName//文件名称
     * @return {*}
     */
    handleDownLoadFileMixins(url, param, fileName) {
      this.$confirm('是否确认导出?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(function() {
        if (fileName) {
          getDownLoadFile(url, param, fileName)
        } else {
          // 无需定义文件名称 且不定文件后缀
          downLoadFileOther(url, param)
        }
      }).catch((err) => {
        console.log(err)
      })
    },

    /**
     * @description: 列表下载文件流
     * @Author: baoxuhui
     * @param {*} dataArr // 附件数组
     * @return {*}
     */
    handleDownLoadMixins(dataArr) {
      this.msgAlt('正在下载,请稍后...', 'success')
      const data = { fileId: dataArr[0].id }
      getDownLoadFile('/sys/annex/handleFileDownload', data, decodeURI(dataArr[0].annexName))
    },

    /**
     * @description: 更多按钮事件
     * @Author: baoxuhui
     * @param {*} command // 指令
     * @param {*} row //需要传入的值
     * @return {*}
     */
    handleCommandMixins(command, row) {
      console.log(command, row)
      if (typeof command === 'string') {
        this[command](row)
      }
      if (typeof command === 'object') {
        // 默认 弹窗新增修改详情
        if (command.typeName) {
          this[command.command](command.type, command.typeName, row)
        }

        // 传路由
        if (command.url) {
          this[command.command](command.type, row, command.url)
        }
        // 传api路径
        if(command.apiUrl) {
          this[command.command](row, command.apiUrl, command.text)
        }
      }
    },
  }
}
